<?php
using("Libs_Catalog");
using("Tests_Databases_Catalog");
using("Db_Common_SqlException");
using("Db_Pdo_PreparedStatement");
using("Db_Pdo_ResultSet");

/**
 * Note that this test assumes that there is more than
 * one row in the CustomAttributes table in order to
 * avoid passing on a failed connection
 */
class Tests_Db_Pdo_PreparedStatement extends UnitTestCase 
{
	/**
	 * PDO Instance
	 * @var PDO
	 */
	private $_dbh;
	
	private static $_query = "SELECT * FROM CustomAttributes";
	
	private static $_query1 = "SELECT * FROM CustomAttributes WHERE key = ?";
	
	private static $_query2 = "INSERT INTO CustomAttributes (key, value) VALUES ('foo', 'bar')";
	
	private static $_query3 = "INSERT INTO CustomAttributes (key, value) VALUES (?, ?)";
	
	private static $_query4 = "INSERT CustomAttributes () VALUES ( ? , ?')";
	
	public function __construct() 
	{
		$this->_dbh = new PDO("sqlite:" . Tests_Databases_Catalog::fetch("test.db"));
	}
	
	public function testExecuteSelectSansParams() 
	{
		
		$stmt = new Db_Pdo_PreparedStatement($this->_dbh->prepare(Tests_Db_Pdo_PreparedStatement::$_query), 
			Tests_Db_Pdo_PreparedStatement::$_query);
		
		$rs = $stmt->executeSelect();
		
		$results = array();
		foreach ($rs as $row) {
			$results[] = $row;
		}
		$this->assertTrue(count($results) > 1);
	}
	
	public function testExecuteSelectWithParams() 
	{
		$stmt = new Db_Pdo_PreparedStatement($this->_dbh->prepare(Tests_Db_Pdo_PreparedStatement::$_query1), 
			Tests_Db_Pdo_PreparedStatement::$_query1);
		
		$rs = $stmt->executeSelect(array("foo"));
		
		$results = array();
		$equal = true;
		foreach ($rs as $row) {
			$results[] = $row;
			if ("foo" !== $row['key']){
				$equal = false;
			}
		}
		$this->assertTrue($equal);
		$this->assertTrue(count($results) > 1);
	}
	
	public function testExecuteSaveSansParams() {
		$stmt = new Db_Pdo_PreparedStatement($this->_dbh->prepare(Tests_Db_Pdo_PreparedStatement::$_query2), 
			Tests_Db_Pdo_PreparedStatement::$_query2);
		
		$this->assertEqual($stmt->executeSave(), 1);
	}
	
	public function testExecuteSave1() {
		$stmt = new Db_Pdo_PreparedStatement($this->_dbh->prepare(Tests_Db_Pdo_PreparedStatement::$_query3), 
			Tests_Db_Pdo_PreparedStatement::$_query3);
		
		$this->assertEqual($stmt->executeSave(array('foo', 'bar')), 1);
	}
	
	public function testExceptions() {
		
		try {
			$stmt = new Db_Pdo_PreparedStatement($this->_dbh->prepare(Tests_Db_Pdo_PreparedStatement::$_query4), 
				Tests_Db_Pdo_PreparedStatement::$_query4);
			
			$stmt->executeSave(array('foo', 'bar'));
			$this->fail("Failed to catch Db_Common_SqlException exception");
		} catch (Db_Common_SqlException $e) {
			$this->pass("Caught Db_Common_SqlException exception");
		}

		try {
			$stmt = new Db_Pdo_PreparedStatement($this->_dbh->prepare(Tests_Db_Pdo_PreparedStatement::$_query1), 
				Tests_Db_Pdo_PreparedStatement::$_query1);
			
			$stmt->executeSave();
			$this->fail("Failed to catch Db_Common_SqlException exception");
		} catch (Db_Common_SqlException $e) {
			$this->pass("Caught Db_Common_SqlException exception");
		}	
		
		try {
			$stmt = new Db_Pdo_PreparedStatement($this->_dbh->prepare(Tests_Db_Pdo_PreparedStatement::$_query3), 
				Tests_Db_Pdo_PreparedStatement::$_query3);
			
			$stmt->executeSelect();
			$this->fail("Failed to catch Db_Common_SqlException exception");
		} catch (Db_Common_SqlException $e) {
			$this->pass("Caught Db_Common_SqlException exception");
		}				
		
	}
	
	
}
?>